package com.songshan.controller;

import com.songshan.bean.User;
import com.songshan.dto.Result;
import com.songshan.exception.BusinessException;

import com.songshan.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author feb
 * @date 2024/6/18
 * @Description: 系统登录和首页
 */
@Controller
@RequestMapping(value = "/springmvc-demo")
public class LoginController {

  @Autowired
  private LoginService loginService;

  /**
   * @author feb
   * @date 2024/6/18
   * @Description: 系统首页
   */
  @RequestMapping(value = "main.htmlx")
  public String main() {
    return "main";
  }

  /**
   * @author feb
   * @date 2024/6/18
   * @Description: 系统登录页面
   */
  @RequestMapping("login.htmlx")
  public String login() {
    return "login";
  }

  /**
   * @author feb
   * @date 2024/6/18
   * @Description: 登录接口
   */
  @PostMapping("login.do")
  @ResponseBody
  public Result login(String username,String password) throws BusinessException{
    User user = loginService.login(username,password);
    Result result = Result.success(user);
    result.setToken(user.getToken());
    return result;
  }

}
